<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="24">
            <a-form-item label="赛事" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['gameId', validatorRules.gameId]" placeholder="请输入赛事" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="选手" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['playerId', validatorRules.playerId]" placeholder="请输入选手" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['playerPhone', validatorRules.playerPhone]" placeholder="请输入联系方式" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="选手姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['playerName', validatorRules.playerName]" placeholder="请输入选手姓名" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="排名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['ranking']" placeholder="请输入排名" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="成绩" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['score']" placeholder="请输入成绩" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="积分" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['integral']" placeholder="请输入积分" style="width: 100%" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: "ZmGamePlayerModal",
    components: {
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          gameId: {
            rules: [
              { required: true, message: '请输入赛事!'},
            ]
          },
          playerId: {
            rules: [
              { required: true, message: '请输入选手!'},
            ]
          },
          playerPhone: {
            rules: [
              { required: true, message: '请输入联系方式!'},
            ]
          },
          playerName: {
            rules: [
              { required: true, message: '请输入选手姓名!'},
            ]
          },
        },
        url: {
          add: "/admin/zmGame/addZmGamePlayer",
          edit: "/admin/zmGame/editZmGamePlayer",
        }

      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'gameId','playerId','playerPhone','playerName','ranking','score','integral'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData['gameId'] = this.mainId
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'gameId','playerId','playerPhone','playerName','ranking','score','integral'))
      },


    }
  }
</script>
